{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"toc_visible":true,"authorship_tag":"ABX9TyM12Eke43elApTrJ7vCwSLU"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# Prompt Caching With Anthropic"],"metadata":{"id":"kFLIivnJpcfu"}},{"cell_type":"markdown","source":["**Prompt caching** is a valuable feature that optimizes API usage by enabling the reuse of specific prompt prefixes.\n","\n","**Benefits**:\n","\n","This method significantly cuts down processing time and costs for tasks or prompts with recurring elements, making it particularly useful for sophisticated LLMs, especially those that are multimodal.\n","\n","\n","\n","**How it works?**\n","\n","- The system checks whether the prompt prefix has been recently cached.\n","\n","- Cache hit: if there is a match, the system will use the cached version of the prompt prefix, thus reducting the cost and the time of processing.\n","\n","- Cache miss: If not, it will process the prompt completely and caches the prefix for future use.\n","\n","\n","**Constraints**:\n","\n","- The prompt prefix must stay the same between various requests to enable prompt caching: Place static data at the start of your prompt message, and position user requests and variable data toward the end.\n","\n","- The prompt has to include at least:\n","1.   1024 tokens for Claude 3.5 Sonnet/ 3 Opus.\n","2.   2048 tokens for Claude 3 Haiku\n","\n","\n","- The cache remains active for 5 minutes.\n","\n","\n","**How to use it?**\n","\n","1.  Specify \"prompt-caching-2024-07-31\" either in the:\n","- API header like this:\n","anthropic-beta: prompt-caching-2024-07-31,\n","or\n","- Directly in the message call like this\n","betas=[\"prompt-caching-2024-07-31\"],\n","\n","2.  Add to your message this parameter:\n","`\"cache_control\": {\"type\": \"ephemeral\"}`"],"metadata":{"id":"F66QvZLfph1o"}},{"cell_type":"markdown","source":["**Use case:**\n","\n","To showcase prompt caching, I'll use the new **\"PDF support\"** feature, which lets me upload a PDF to Claude 3.5 Sonnet. This is an ideal use case, as the PDF content is static. I'll first include the PDF content in the message to enable prefix caching, then add my query at the end.\n","\n","\n","\n","👉 We'll use **BlackRock Q4 2024 Outlooks report:**. This report outlines BlackRock's key investment strategies across three main areas: equities, fixed income , and alternative assets. The document provides detailed analysis and recommendations for positioning portfolios amid market volatility, falling interest rates, and the upcoming US presidential election, while emphasizing the importance of maintaining a selective approach across asset classes."],"metadata":{"id":"gmd6D3dlGjVs"}},{"cell_type":"markdown","source":["Original report: https://www.blackrock.com/institutions/en-zz/literature/whitepaper/investment-directions-q4-24-np.pdf\n","\n","I excluded the last 6 pages, because they have no additional value."],"metadata":{"id":"nt-gIUnDJqrk"}},{"cell_type":"markdown","source":["[Hanane Dupouy](https://www.linkedin.com/in/hanane-d-algo-trader)"],"metadata":{"id":"dSS1K0yvSVg3"}},{"cell_type":"markdown","source":["# Install libs"],"metadata":{"id":"N77xnKia09-3"}},{"cell_type":"code","source":["!pip install anthropic -q"],"metadata":{"id":"_9rfdqXL2JIz"},"execution_count":null,"outputs":[]},{"cell_type":"code","execution_count":16,"metadata":{"id":"lNVVN_Gh1eQq","executionInfo":{"status":"ok","timestamp":1730806789606,"user_tz":-60,"elapsed":490,"user":{"displayName":"Hanane","userId":"07422163243842727239"}}},"outputs":[],"source":["import anthropic\n","import base64\n","from IPython.display import HTML"]},{"cell_type":"code","source":["from google.colab import userdata\n","CLAUDE_API_KEY = userdata.get('CLAUDE_API_KEY')"],"metadata":{"id":"jKuxCyDD3DCq","executionInfo":{"status":"ok","timestamp":1730806048740,"user_tz":-60,"elapsed":1758,"user":{"displayName":"Hanane","userId":"07422163243842727239"}}},"execution_count":3,"outputs":[]},{"cell_type":"markdown","source":["# PDF loading"],"metadata":{"id":"Ce8Dst4v1GlY"}},{"cell_type":"code","source":["def upload_pdf(path_to_pdf):\n","  with open(path_to_pdf, \"rb\") as pdf_file:\n","    binary_data = pdf_file.read()\n","    base_64_encoded_data = base64.b64encode(binary_data)\n","    base64_string = base_64_encoded_data.decode('utf-8')\n","  return base64_string"],"metadata":{"id":"NsCVuetv3eFu","executionInfo":{"status":"ok","timestamp":1730806803580,"user_tz":-60,"elapsed":455,"user":{"displayName":"Hanane","userId":"07422163243842727239"}}},"execution_count":18,"outputs":[]},{"cell_type":"code","source":["# BlackRock pdf from: https://www.blackrock.com/institutions/en-zz/literature/whitepaper/investment-directions-q4-24-np.pdf\n","# Investment Directions\n","# Q4 2024: Exposures for today’s market\n","# Key Themes and Outlook for Q4 2024 by BlackRock\n","# I exluded the 6 last pages, because a lot of text about risk warnings in several countries where BlackRock is present, without any added values for Q4 2024 outlooks\n","\n","path_to_pdf = local_path + 'BlackRock_investment-directions-q4-24-np-1-13.pdf'\n","pdf_data = upload_pdf(path_to_pdf)"],"metadata":{"id":"jL1jgxwxipSc","executionInfo":{"status":"ok","timestamp":1730806810166,"user_tz":-60,"elapsed":1140,"user":{"displayName":"Hanane","userId":"07422163243842727239"}}},"execution_count":20,"outputs":[]},{"cell_type":"markdown","source":["# Define completion method with Prompt Caching feature:"],"metadata":{"id":"a4kMRIMHMtbY"}},{"cell_type":"markdown","source":["In the message completion creation level, I'll specify the usage of the new feature supporting PDF upload **\"pdfs-2024-09-25\"**, alongside the prompt caching feature **\"prompt-caching-2024-07-31\"**.\n"],"metadata":{"id":"zOBc1dRIM1P9"}},{"cell_type":"code","source":["client = anthropic.Anthropic(api_key = CLAUDE_API_KEY)\n","# For now, only claude-3-5-sonnet-20241022 supports PDFs\n","MODEL_NAME = \"claude-3-5-sonnet-20241022\"\n","\n","def get_completion(messages, model=MODEL_NAME):\n","    completion = client.beta.messages.create(\n","        betas=[\"pdfs-2024-09-25\", \"prompt-caching-2024-07-31\"],\n","        model=model,\n","        max_tokens=8192,\n","        messages=messages,\n","        temperature=0,\n","    )\n","    return completion"],"metadata":{"id":"Aa87b7SSMwOO","executionInfo":{"status":"ok","timestamp":1730806818182,"user_tz":-60,"elapsed":375,"user":{"displayName":"Hanane","userId":"07422163243842727239"}}},"execution_count":21,"outputs":[]},{"cell_type":"markdown","source":["# Questions to ask"],"metadata":{"id":"G1tQJQkz5r3C"}},{"cell_type":"code","source":["queries = [\"What are the main investment themes discussed in the Q4 2024 outlook?\",\n","\"What are the key takeaways from BlackRock’s investment strategies for Q4 2024?\",\n","\"Which sectors are expected to benefit most from the AI build-out, according to BlackRock?\",\n","\"How might the upcoming U.S. presidential election impact investment strategies?\",\n","\"What are BlackRock’s views on the impact of trade policies and economic fragmentation on inflation?\",\n","\"How does BlackRock suggest positioning a portfolio to mitigate geopolitical risks?\"]"],"metadata":{"id":"hok8-blX5t4a","executionInfo":{"status":"ok","timestamp":1730806077109,"user_tz":-60,"elapsed":3,"user":{"displayName":"Hanane","userId":"07422163243842727239"}}},"execution_count":6,"outputs":[]},{"cell_type":"markdown","source":["Before start chating and using the prompt caching, let's count the number of tokens in the pdf:"],"metadata":{"id":"vyHGznE555MD"}},{"cell_type":"markdown","source":["# Counting Tokens"],"metadata":{"id":"DOgglG0M4br4"}},{"cell_type":"markdown","source":["You are not charged when asking for tokens counting"],"metadata":{"id":"Uf8PyPyf_XER"}},{"cell_type":"code","source":["client = anthropic.Anthropic(api_key = CLAUDE_API_KEY)\n","\n","response = client.beta.messages.count_tokens(\n","    betas=[\"token-counting-2024-11-01\", \"pdfs-2024-09-25\"],\n","    model=\"claude-3-5-sonnet-20241022\",\n","    messages=[{\n","        \"role\": \"user\",\n","        \"content\": [\n","            {\"type\": \"document\", \"source\": { \"type\": \"base64\", \"media_type\": \"application/pdf\", \"data\": pdf_data}},\n","            {\"type\": \"text\", \"text\": \"Summarize\"}\n","        ]\n","    }]\n",")\n","\n","print(response.json())"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"gpPhaGjD4bJQ","executionInfo":{"status":"ok","timestamp":1730786583772,"user_tz":-60,"elapsed":859,"user":{"displayName":"Hanane","userId":"07422163243842727239"}},"outputId":"83ff14d3-99c1-43ee-ed37-89eb1bea8af1"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["{\"input_tokens\":31039}\n"]}]},{"cell_type":"markdown","source":["It also counts the text message, but it's a tiny number of tokens."],"metadata":{"id":"ZFPXD1-qMUuj"}},{"cell_type":"code","source":["response"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"7gnlRdRW6daO","executionInfo":{"status":"ok","timestamp":1730786673032,"user_tz":-60,"elapsed":218,"user":{"displayName":"Hanane","userId":"07422163243842727239"}},"outputId":"7c66de9a-4cf8-4c34-9000-861767ab0afe"},"execution_count":null,"outputs":[{"output_type":"execute_result","data":{"text/plain":["BetaMessageTokensCount(input_tokens=31039)"]},"metadata":{},"execution_count":10}]},{"cell_type":"markdown","source":["Without the PDF:"],"metadata":{"id":"ozcN7R2u7kFQ"}},{"cell_type":"code","source":["client = anthropic.Anthropic(api_key = CLAUDE_API_KEY)\n","\n","response = client.beta.messages.count_tokens(\n","    betas=[\"token-counting-2024-11-01\", \"pdfs-2024-09-25\"],\n","    model=\"claude-3-5-sonnet-20241022\",\n","    messages=[{\n","        \"role\": \"user\",\n","        \"content\": [\n","          {\"type\": \"text\", \"text\": \"Summarize\"}\n","        ]\n","    }]\n",")\n","\n","print(response.json())"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"9duJTaBU7Mul","executionInfo":{"status":"ok","timestamp":1730786685448,"user_tz":-60,"elapsed":463,"user":{"displayName":"Hanane","userId":"07422163243842727239"}},"outputId":"7d713183-689c-4aa2-8956-06f16cb231c7"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["{\"input_tokens\":10}\n"]}]},{"cell_type":"markdown","source":["# Prompt Caching:"],"metadata":{"id":"5gW4Awd57ppo"}},{"cell_type":"markdown","source":["Let's recall here the completion method with both PDF support and prompt caching features:"],"metadata":{"id":"7CjZ-fGbGd3f"}},{"cell_type":"code","source":["client = anthropic.Anthropic(api_key = CLAUDE_API_KEY)\n","MODEL_NAME = \"claude-3-5-sonnet-20241022\"\n","\n","def get_completion(messages, model=MODEL_NAME):\n","    completion = client.beta.messages.create(\n","        betas=[\"pdfs-2024-09-25\", \"prompt-caching-2024-07-31\"],\n","        model=model,\n","        max_tokens=8192,\n","        messages=messages,\n","        temperature=0,\n","    )\n","    return completion"],"metadata":{"id":"Rz2d_BO-99Jq","executionInfo":{"status":"ok","timestamp":1730806738563,"user_tz":-60,"elapsed":407,"user":{"displayName":"Hanane","userId":"07422163243842727239"}}},"execution_count":14,"outputs":[]},{"cell_type":"markdown","source":["In the following, I'll compare the cost between using and not the prompt caching feature:"],"metadata":{"id":"v1f2WxyGNjYs"}},{"cell_type":"markdown","source":["## Without Prompt Caching"],"metadata":{"id":"x89l6NrVCNIC"}},{"cell_type":"markdown","source":["I'm using the standard completion message, without the caching parameter:"],"metadata":{"id":"ab4HJGLqGUed"}},{"cell_type":"code","source":["def build_message (query, pdf_data):\n","  messages = [\n","      {\n","          \"role\": 'user',\n","          \"content\": [\n","              {\"type\": \"document\", \"source\": {\"type\": \"base64\", \"media_type\": \"application/pdf\", \"data\": pdf_data}},\n","              {\"type\": \"text\", \"text\": query}\n","          ]\n","      }\n","    ]\n","  return messages"],"metadata":{"id":"2hZWaF5h9AOD"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["I'll send and request Claude 3.5 Sonnet to answer 6 questions based on the BlackRock report:"],"metadata":{"id":"Q8sZFjxyNzyZ"}},{"cell_type":"code","source":["for idx, query in enumerate(queries):\n","  display(HTML(f\"<p style='color:red;'>Query n° {idx+1}</p>\"))\n","  messages = build_message(query, pdf_data)\n","  print(\"--------QUERY---------\")\n","  display(HTML(f\"<p style='color:fuchsia;'>--------QUERY---------</p>\"))\n","  display(HTML(f\"<p style='color:fuchsia;'>{query}</p>\"))\n","  print()\n","  completion = get_completion(messages)\n","  display(HTML(f\"<p style='color:green;'>--------ANSWER---------</p>\"))\n","  print(completion.content[0].text)\n","  print()\n","  display(HTML(f\"<p style='color:blue;'>--------TOKENS COUNT---------</p>\"))\n","  display(HTML(f\"<p style='color:blue;'>{completion.usage}</p>\"))\n","  # print(completion.usage)\n","  print()\n","  print(\"--------STOP REASON---------\")\n","  print(completion.stop_reason)\n","  print(\"\\n\\n\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":1000},"id":"7cy0wBdQ82Kp","executionInfo":{"status":"error","timestamp":1730788684325,"user_tz":-60,"elapsed":101153,"user":{"displayName":"Hanane","userId":"07422163243842727239"}},"outputId":"d0fcffe8-dc4a-446c-f093-499d5ca06bac"},"execution_count":null,"outputs":[{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:red;'>Query n° 1</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["--------QUERY---------\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>--------QUERY---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>What are the main investment themes discussed in the Q4 2024 outlook?</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:green;'>--------ANSWER---------</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["Based on the Q4 2024 investment outlook document, here are the main investment themes discussed:\n","\n","1. Quality and Breadth in Equities:\n","- Looking to take advantage of broadening earnings prospects in developed markets\n","- Maintaining a high-quality tilt and selectivity amid volatility\n","- Favoring a granular, active approach, especially in Europe\n","- Continuing to see tailwinds for AI beneficiaries\n","\n","2. Fixed Income Opportunities:\n","- Recommending locking in income while rates remain elevated before further rate cuts\n","- Favoring the belly of the US Treasury curve\n","- More comfortable extending duration in European bonds\n","- Preference for EUR credit, especially quality credit, though opportunities exist in high yield\n","\n","3. Alternative Assets and Diversification:\n","- Calling for a broader set of diversifiers due to macro uncertainty\n","- Looking to liquid alternatives to capture potential alpha in volatile markets\n","- Seeing strategic opportunities to address portfolio underweights in private markets\n","- Bonds regaining their traditional role as portfolio ballast\n","\n","4. Key Market Context:\n","- Central banks (Fed, ECB, BoE) beginning easing cycles but at different paces\n","- Remaining pro-risk while expecting continued macro volatility\n","- Preparing for potential volatility around the US presidential election\n","- Shifting from narrow US tech focus to broader market opportunities\n","\n","5. Regional Preferences:\n","- Maintaining conviction in US equities while looking for select European opportunities\n","- Taking a selective approach to emerging markets\n","- Seeing strong opportunity set in Europe despite economic challenges\n","\n","The outlook emphasizes managing volatility while positioning for broadening market opportunities and the start of global easing cycles, with a focus on quality and selective positioning across asset classes.\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>--------TOKENS COUNT---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>BetaUsage(cache_creation_input_tokens=0, cache_read_input_tokens=0, input_tokens=31053, output_tokens=374)</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n","--------STOP REASON---------\n","end_turn\n","\n","\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:red;'>Query n° 2</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["--------QUERY---------\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>--------QUERY---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>What are the key takeaways from BlackRock’s investment strategies for Q4 2024?</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:green;'>--------ANSWER---------</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["Based on the document, here are the key takeaways from BlackRock's Q4 2024 investment strategies:\n","\n","1. Equity Strategy:\n","- Focus on quality and breadth in equities\n","- Taking advantage of broadening earnings prospects in developed markets\n","- Maintaining high-quality tilt and selectivity amid volatility\n","- Strong opportunity set seen in Europe\n","- Continued positive outlook for AI beneficiaries\n","\n","2. Fixed Income Strategy:\n","- Recommends locking in income while rates remain elevated\n","- Favors the belly of the US Treasury curve\n","- More comfortable extending duration in European bonds\n","- Preference for EUR credit over USD credit\n","- Focus on quality in credit markets, though opportunities exist in high yield\n","\n","3. Alternative Assets/Diversifiers:\n","- Increased focus on alternative assets for diversification\n","- Looking to liquid alternatives to capture potential alpha in volatile markets\n","- Strategic opportunity to increase allocation to private markets\n","- Gold seen as potential hedge against geopolitical risks\n","- Multi-asset strategies recommended for navigating market complexity\n","\n","4. Key Market Context:\n","- Expecting volatility around US presidential election\n","- Central banks beginning easing cycles but at different paces\n","- Macro uncertainty remains elevated\n","- Cross-asset correlations less predictable than in the past\n","- China stimulus providing some tailwinds for certain sectors\n","\n","5. Regional Views:\n","- Maintained conviction in US equities\n","- Selective opportunities in Europe\n","- Granular approach recommended for emerging markets\n","- Positive outlook for India due to low correlation with global equities\n","\n","The overall strategy emphasizes quality, diversification, and active management while positioning for both opportunities and risks in an environment of falling rates and potential volatility.\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>--------TOKENS COUNT---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>BetaUsage(cache_creation_input_tokens=0, cache_read_input_tokens=0, input_tokens=31057, output_tokens=370)</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n","--------STOP REASON---------\n","end_turn\n","\n","\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:red;'>Query n° 3</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["--------QUERY---------\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>--------QUERY---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>Which sectors are expected to benefit most from the AI build-out, according to BlackRock?</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:green;'>--------ANSWER---------</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["According to the document, several sectors are expected to benefit from the AI build-out:\n","\n","1. Utilities - The document notes that utilities are well-positioned to benefit from AI's expansion due to:\n","- The massive power requirements of AI infrastructure (noting that a data center using a 100k GPU cluster would require a small power plant to run)\n","- The sector's strong tilt toward public infrastructure needed to support AI development\n","- Power capacity requirements putting a cap on AI growth\n","\n","2. Infrastructure - Particularly public infrastructure which is mentioned as being well-positioned for the AI-driven demand\n","\n","3. Sustainable Energy - The document indicates sustainable energy exposures look well-positioned due to:\n","- The significant power demands of AI\n","- Advancing technology\n","- Declining costs \n","- Supportive policy environment\n","\n","The document emphasizes that markets may be underestimating the scale of capital expenditure and power needs required for the AI build-out phase, particularly as companies invest in larger GPU clusters and more advanced AI models requiring greater power capacity. This creates opportunities in sectors that can support this infrastructure and power-intensive expansion.\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>--------TOKENS COUNT---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>BetaUsage(cache_creation_input_tokens=0, cache_read_input_tokens=0, input_tokens=31056, output_tokens=244)</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n","--------STOP REASON---------\n","end_turn\n","\n","\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:red;'>Query n° 4</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["--------QUERY---------\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>--------QUERY---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>How might the upcoming U.S. presidential election impact investment strategies?</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:green;'>--------ANSWER---------</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["Based on the document, here are the key potential investment impacts of the upcoming US presidential election:\n","\n","1. Trade Policy Implications:\n","- Both candidates are likely to pursue additional export controls, especially in advanced technology\n","- Harris is expected to maintain current tariff policies with potential targeted China tariffs\n","- Trump proposes more aggressive tariffs (60% on China, 10-20% broad tariffs)\n","- Increased protectionism under either administration could contribute to structural inflation\n","\n","2. Market Volatility:\n","- Election uncertainty could lead to increased market volatility heading into November\n","- Alpha-seeking strategies could help capitalize on stock-level dispersion during volatile periods\n","- UK equities are suggested as a potential hedge against trade rhetoric, given their low beta to shifts in global trade\n","\n","3. Fiscal Policy:\n","- Neither candidate has prioritized addressing the budget deficit\n","- Analysis suggests either candidate could pursue fiscally expansionary policies\n","- This could bring volatility to the long end of the US Treasury curve\n","- Higher fiscal deficits could lead to increased bond issuance and higher 10-year yields\n","\n","4. Portfolio Strategies:\n","- The document suggests considering:\n","  - Quality-focused investments to manage volatility\n","  - Active management approaches to navigate uncertainty\n","  - Diversification through alternative assets\n","  - Maintaining selective positioning across asset classes\n","\n","The document emphasizes that control of Congress will be key for policy implementation, and suggests investors should prepare for potential market volatility while maintaining a selective, quality-focused approach across their portfolios.\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>--------TOKENS COUNT---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>BetaUsage(cache_creation_input_tokens=0, cache_read_input_tokens=0, input_tokens=31051, output_tokens=337)</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n","--------STOP REASON---------\n","end_turn\n","\n","\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:red;'>Query n° 5</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["--------QUERY---------\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>--------QUERY---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>What are BlackRock’s views on the impact of trade policies and economic fragmentation on inflation?</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n"]},{"output_type":"error","ename":"RateLimitError","evalue":"Error code: 429 - {'type': 'error', 'error': {'type': 'rate_limit_error', 'message': 'Number of request tokens has exceeded your per-minute rate limit (https://docs.anthropic.com/en/api/rate-limits); see the response headers for current usage. Please reduce the prompt length or the maximum tokens requested, or try again later. You may also contact sales at https://www.anthropic.com/contact-sales to discuss your options for a rate limit increase.'}}","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mRateLimitError\u001b[0m                            Traceback (most recent call last)","\u001b[0;32m<ipython-input-51-8991d48590d4>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      6\u001b[0m   \u001b[0mdisplay\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mHTML\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"<p style='color:fuchsia;'>{query}</p>\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m   \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m   \u001b[0mcompletion\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_completion\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmessages\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      9\u001b[0m   \u001b[0mdisplay\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mHTML\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"<p style='color:green;'>--------ANSWER---------</p>\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     10\u001b[0m   \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcompletion\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcontent\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtext\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m<ipython-input-49-0c3e8bde72ec>\u001b[0m in \u001b[0;36mget_completion\u001b[0;34m(messages, model)\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     15\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mget_completion\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmessages\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mMODEL_NAME\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m     completion = client.beta.messages.create(\n\u001b[0m\u001b[1;32m     17\u001b[0m         \u001b[0mbetas\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"pdfs-2024-09-25\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"prompt-caching-2024-07-31\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     18\u001b[0m         \u001b[0mmodel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.10/dist-packages/anthropic/_utils/_utils.py\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m    273\u001b[0m                         \u001b[0mmsg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34mf\"Missing required argument: {quote(missing[0])}\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    274\u001b[0m                 \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 275\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    276\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    277\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mwrapper\u001b[0m  \u001b[0;31m# type: ignore\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.10/dist-packages/anthropic/resources/beta/messages/messages.py\u001b[0m in \u001b[0;36mcreate\u001b[0;34m(self, max_tokens, messages, model, metadata, stop_sequences, stream, system, temperature, tool_choice, tools, top_k, top_p, betas, extra_headers, extra_query, extra_body, timeout)\u001b[0m\n\u001b[1;32m    896\u001b[0m             \u001b[0;34m**\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mextra_headers\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    897\u001b[0m         }\n\u001b[0;32m--> 898\u001b[0;31m         return self._post(\n\u001b[0m\u001b[1;32m    899\u001b[0m             \u001b[0;34m\"/v1/messages?beta=true\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    900\u001b[0m             body=maybe_transform(\n","\u001b[0;32m/usr/local/lib/python3.10/dist-packages/anthropic/_base_client.py\u001b[0m in \u001b[0;36mpost\u001b[0;34m(self, path, cast_to, body, options, files, stream, stream_cls)\u001b[0m\n\u001b[1;32m   1275\u001b[0m             \u001b[0mmethod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"post\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mjson_data\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mbody\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfiles\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mto_httpx_files\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfiles\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0moptions\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1276\u001b[0m         )\n\u001b[0;32m-> 1277\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mcast\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mResponseT\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcast_to\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mopts\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstream\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mstream\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstream_cls\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mstream_cls\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1278\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1279\u001b[0m     def patch(\n","\u001b[0;32m/usr/local/lib/python3.10/dist-packages/anthropic/_base_client.py\u001b[0m in \u001b[0;36mrequest\u001b[0;34m(self, cast_to, options, remaining_retries, stream, stream_cls)\u001b[0m\n\u001b[1;32m    952\u001b[0m             \u001b[0mretries_taken\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    953\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 954\u001b[0;31m         return self._request(\n\u001b[0m\u001b[1;32m    955\u001b[0m             \u001b[0mcast_to\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcast_to\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    956\u001b[0m             \u001b[0moptions\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0moptions\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.10/dist-packages/anthropic/_base_client.py\u001b[0m in \u001b[0;36m_request\u001b[0;34m(self, cast_to, options, retries_taken, stream, stream_cls)\u001b[0m\n\u001b[1;32m   1041\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mremaining_retries\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_should_retry\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1042\u001b[0m                 \u001b[0merr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1043\u001b[0;31m                 return self._retry_request(\n\u001b[0m\u001b[1;32m   1044\u001b[0m                     \u001b[0minput_options\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1045\u001b[0m                     \u001b[0mcast_to\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.10/dist-packages/anthropic/_base_client.py\u001b[0m in \u001b[0;36m_retry_request\u001b[0;34m(self, options, cast_to, retries_taken, response_headers, stream, stream_cls)\u001b[0m\n\u001b[1;32m   1090\u001b[0m         \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1091\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1092\u001b[0;31m         return self._request(\n\u001b[0m\u001b[1;32m   1093\u001b[0m             \u001b[0moptions\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0moptions\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1094\u001b[0m             \u001b[0mcast_to\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcast_to\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.10/dist-packages/anthropic/_base_client.py\u001b[0m in \u001b[0;36m_request\u001b[0;34m(self, cast_to, options, retries_taken, stream, stream_cls)\u001b[0m\n\u001b[1;32m   1041\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mremaining_retries\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_should_retry\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1042\u001b[0m                 \u001b[0merr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1043\u001b[0;31m                 return self._retry_request(\n\u001b[0m\u001b[1;32m   1044\u001b[0m                     \u001b[0minput_options\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1045\u001b[0m                     \u001b[0mcast_to\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.10/dist-packages/anthropic/_base_client.py\u001b[0m in \u001b[0;36m_retry_request\u001b[0;34m(self, options, cast_to, retries_taken, response_headers, stream, stream_cls)\u001b[0m\n\u001b[1;32m   1090\u001b[0m         \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1091\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1092\u001b[0;31m         return self._request(\n\u001b[0m\u001b[1;32m   1093\u001b[0m             \u001b[0moptions\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0moptions\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1094\u001b[0m             \u001b[0mcast_to\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcast_to\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.10/dist-packages/anthropic/_base_client.py\u001b[0m in \u001b[0;36m_request\u001b[0;34m(self, cast_to, options, retries_taken, stream, stream_cls)\u001b[0m\n\u001b[1;32m   1056\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1057\u001b[0m             \u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdebug\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Re-raising status error\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1058\u001b[0;31m             \u001b[0;32mraise\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_make_status_error_from_response\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1059\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1060\u001b[0m         return self._process_response(\n","\u001b[0;31mRateLimitError\u001b[0m: Error code: 429 - {'type': 'error', 'error': {'type': 'rate_limit_error', 'message': 'Number of request tokens has exceeded your per-minute rate limit (https://docs.anthropic.com/en/api/rate-limits); see the response headers for current usage. Please reduce the prompt length or the maximum tokens requested, or try again later. You may also contact sales at https://www.anthropic.com/contact-sales to discuss your options for a rate limit increase.'}}"]}]},{"cell_type":"markdown","source":["### **Takeaways**"],"metadata":{"id":"fY3n18M9OIDu"}},{"cell_type":"markdown","source":["**RateLimit:**\n","\n","I have a RateLimitError in the query number 5, because I exceeded tne authorized number of tokens per minute: As the document was not cached, it was sent over and over (31K tokens)\n","\n","`{'type': 'rate_limit_error', 'message': 'Number of request tokens has exceeded your per-minute rate limit; see the response headers for current usage. Please reduce the prompt length or the maximum tokens requested, or try again later....`"],"metadata":{"id":"YrvUZO5dF-Zg"}},{"cell_type":"markdown","source":["**Cost and Number of tokens:**\n","\n","*   I have done 4 calls with 125542 tokens. ==> which corresponds to 31 385 tokens per call (Coherent with the tokens counting we performed before).\n","*   This costs me 0.40\\$==> **0.10\\$/request**.\n","*   For each request I'm sending 31050 tokens and getting 370 tokens output."],"metadata":{"id":"GLAcRUnHGod_"}},{"cell_type":"markdown","source":["## With Prompt Caching\n","\n","\n"],"metadata":{"id":"PvvzfbqT-r3Q"}},{"cell_type":"markdown","source":["You need to add this prompt caching parameter to your message:\n","`\"cache_control\": {\"type\": \"ephemeral\"}`"],"metadata":{"id":"-H0dSva7HXT4"}},{"cell_type":"code","source":["def build_message_prompt_caching (query, pdf_data):\n","  messages = [\n","      {\n","          \"role\": 'user',\n","          \"content\": [\n","              {\"type\": \"document\", \"source\": {\"type\": \"base64\", \"media_type\": \"application/pdf\", \"data\": pdf_data},\"cache_control\": {\"type\": \"ephemeral\"}},\n","              {\"type\": \"text\", \"text\": query}\n","          ]\n","      }\n","    ]\n","  return messages"],"metadata":{"id":"Bfki-I0aB4w4","executionInfo":{"status":"ok","timestamp":1730806869524,"user_tz":-60,"elapsed":426,"user":{"displayName":"Hanane","userId":"07422163243842727239"}}},"execution_count":22,"outputs":[]},{"cell_type":"markdown","source":["First, I'll send only the first query, to trigger the caching (you can send all the queries at once), to show the completion.usage object:"],"metadata":{"id":"8UvQ1-txIeYv"}},{"cell_type":"code","source":["for idx, query in enumerate(queries[:1]):\n","  display(HTML(f\"<p style='color:red;'>Query n° {idx+1}</p>\"))\n","  messages = build_message_prompt_caching(query, pdf_data)\n","  print(\"--------QUERY---------\")\n","  display(HTML(f\"<p style='color:fuchsia;'>--------QUERY---------</p>\"))\n","  display(HTML(f\"<p style='color:fuchsia;'>{query}</p>\"))\n","  print()\n","  completion = get_completion(messages)\n","  display(HTML(f\"<p style='color:green;'>--------ANSWER---------</p>\"))\n","  print(completion.content[0].text)\n","  print()\n","  display(HTML(f\"<p style='color:blue;'>--------TOKENS COUNT---------</p>\"))\n","  display(HTML(f\"<p style='color:blue;'>{completion.usage}</p>\"))\n","  # print(completion.usage)\n","  print()\n","  print(\"--------STOP REASON---------\")\n","  print(completion.stop_reason)\n","  print(\"\\n\\n\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":912},"id":"2QjPX0_3HyQU","executionInfo":{"status":"ok","timestamp":1730806890833,"user_tz":-60,"elapsed":18552,"user":{"displayName":"Hanane","userId":"07422163243842727239"}},"outputId":"10546e72-6b01-48a2-86b3-d0b13282323d"},"execution_count":23,"outputs":[{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:red;'>Query n° 1</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["--------QUERY---------\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>--------QUERY---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>What are the main investment themes discussed in the Q4 2024 outlook?</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:green;'>--------ANSWER---------</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["Based on the Q4 2024 investment outlook document, here are the main investment themes discussed:\n","\n","1. Quality and Breadth in Equities:\n","- Looking to take advantage of broadening earnings prospects in developed markets\n","- Maintaining a high-quality tilt and selectivity amid volatility\n","- Favoring a granular, active approach, especially in Europe\n","- Continuing to see tailwinds for AI beneficiaries\n","\n","2. Fixed Income Opportunities:\n","- Recommending locking in income while rates remain elevated before further rate cuts\n","- Favoring the belly of the US Treasury curve\n","- More comfortable extending duration in European bonds\n","- Preference for EUR credit, especially quality credit, though opportunities exist in high yield\n","\n","3. Alternative Assets and Diversification:\n","- Calling for a broader set of diversifiers due to macro uncertainty\n","- Looking to liquid alternatives to capture potential alpha in volatile markets\n","- Seeing strategic opportunities to address portfolio underweights in private markets\n","- Bonds regaining their traditional role as portfolio ballast\n","\n","4. Key Market Context:\n","- Central banks (Fed, ECB, BoE) beginning easing cycles but at different paces\n","- Remaining pro-risk while expecting continued macro volatility\n","- Preparing for potential volatility around the US presidential election\n","- Shifting from narrow US tech focus to broader market opportunities\n","\n","5. Regional Preferences:\n","- Maintaining conviction in US equities while looking for select European opportunities\n","- Taking a selective approach to emerging markets\n","- Seeing strong opportunity set in Europe despite economic challenges\n","\n","The outlook emphasizes managing volatility while positioning for broadening market opportunities and the start of global easing cycles, with a focus on quality and selective positioning across asset classes.\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>--------TOKENS COUNT---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>BetaUsage(cache_creation_input_tokens=31019, cache_read_input_tokens=0, input_tokens=34, output_tokens=374)</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n","--------STOP REASON---------\n","end_turn\n","\n","\n","\n"]}]},{"cell_type":"markdown","source":["\n","```\n","--------TOKENS COUNT---------\n","\n","BetaUsage(cache_creation_input_tokens=31019, cache_read_input_tokens=0, input_tokens=34, output_tokens=374)\n","\n","```\n","\n","As you can see the prompting cache feature has created a cache of 31019 tokens of the document in *cache_creation_input_tokens*.\n","\n","\n","- In the following requestes this cache will be used (during 5 mins).\n","\n","- Pay attention to the paragraph \"--------TOKENS COUNT---------\"\n","\n"],"metadata":{"id":"qoWpiI5dI09i"}},{"cell_type":"code","source":["for idx, query in enumerate(queries[1:]):\n","  display(HTML(f\"<p style='color:red;'>Query n° {idx+1}</p>\"))\n","  messages = build_message_prompt_caching(query, pdf_data)\n","  print(\"--------QUERY---------\")\n","  display(HTML(f\"<p style='color:fuchsia;'>--------QUERY---------</p>\"))\n","  display(HTML(f\"<p style='color:fuchsia;'>{query}</p>\"))\n","  print()\n","  completion = get_completion(messages)\n","  display(HTML(f\"<p style='color:green;'>--------ANSWER---------</p>\"))\n","  print(completion.content[0].text)\n","  print()\n","  display(HTML(f\"<p style='color:blue;'>--------TOKENS COUNT---------</p>\"))\n","  display(HTML(f\"<p style='color:blue;'>{completion.usage}</p>\"))\n","  # print(completion.usage)\n","  print()\n","  print(\"--------STOP REASON---------\")\n","  print(completion.stop_reason)\n","  print(\"\\n\\n\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":1000},"id":"oLvnzLTo-oQ_","executionInfo":{"status":"ok","timestamp":1730807178070,"user_tz":-60,"elapsed":271413,"user":{"displayName":"Hanane","userId":"07422163243842727239"}},"outputId":"ed3ab906-edfb-4698-f6c8-d860fbc41738"},"execution_count":24,"outputs":[{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:red;'>Query n° 1</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["--------QUERY---------\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>--------QUERY---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>What are the key takeaways from BlackRock’s investment strategies for Q4 2024?</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:green;'>--------ANSWER---------</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["Based on the document, here are the key takeaways from BlackRock's Q4 2024 investment strategies:\n","\n","1. Equity Strategy:\n","- Focus on quality and breadth in equities\n","- Taking advantage of broadening earnings prospects in developed markets\n","- Maintaining high-quality tilt and selectivity amid volatility\n","- Strong opportunity set seen in Europe\n","- Continued positive outlook for AI beneficiaries\n","\n","2. Fixed Income Strategy:\n","- Recommends locking in income while rates remain elevated\n","- Favors the belly of the US Treasury curve\n","- More comfortable extending duration in European bonds\n","- Preference for EUR credit over USD credit\n","- Focus on quality in credit markets, though opportunities exist in high yield\n","\n","3. Alternative Assets/Diversifiers:\n","- Increased focus on alternative assets for diversification\n","- Looking to liquid alternatives to capture potential alpha in volatile markets\n","- Strategic opportunity to increase allocation to private markets\n","- Gold seen as potential hedge against geopolitical risks\n","- Multi-asset strategies recommended for navigating market complexity\n","\n","4. Key Market Context:\n","- Expecting volatility around US presidential election\n","- Central banks beginning easing cycles but at different paces\n","- Macro uncertainty remains elevated\n","- Cross-asset correlations less predictable than in the past\n","- China stimulus providing some tailwinds for certain sectors\n","\n","5. Regional Views:\n","- Maintained conviction in US equities\n","- Selective opportunities in Europe\n","- Granular approach recommended for emerging markets\n","- Positive outlook for India due to low correlation with global equities\n","\n","The overall strategy emphasizes quality, diversification, and active management while positioning for both opportunities and risks in an environment of falling rates and potential volatility.\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>--------TOKENS COUNT---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>BetaUsage(cache_creation_input_tokens=0, cache_read_input_tokens=31019, input_tokens=38, output_tokens=378)</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n","--------STOP REASON---------\n","end_turn\n","\n","\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:red;'>Query n° 2</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["--------QUERY---------\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>--------QUERY---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>Which sectors are expected to benefit most from the AI build-out, according to BlackRock?</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:green;'>--------ANSWER---------</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["According to the document, BlackRock identifies several key sectors that are expected to benefit from the AI build-out:\n","\n","1. Utilities - The document notes that utilities are well-positioned to benefit from AI's expansion due to:\n","- The massive power requirements of AI infrastructure (noting that a data center using a 100k GPU cluster would require a small power plant to run)\n","- The sector's strong tilt toward public infrastructure needed to support AI development\n","- Power capacity requirements putting a cap on AI growth\n","\n","2. Infrastructure - Particularly:\n","- Public infrastructure that will be needed to support AI development\n","- Infrastructure required to power the vast AI expansion\n","- Listed infrastructure exposures that provide liquid access to infrastructure sub-sectors\n","\n","3. Sustainable Energy - The document mentions sustainable energy exposures are positioned to benefit due to:\n","- The significant power demands of AI\n","- Advancing technology\n","- Declining costs\n","- Supportive policy environment\n","\n","The document emphasizes that markets may be underestimating the scale of capital expenditure and power needs required for the AI build-out phase, particularly as companies invest in larger GPU clusters and more advanced AI models requiring significant infrastructure investment.\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>--------TOKENS COUNT---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>BetaUsage(cache_creation_input_tokens=0, cache_read_input_tokens=31019, input_tokens=37, output_tokens=250)</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n","--------STOP REASON---------\n","end_turn\n","\n","\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:red;'>Query n° 3</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["--------QUERY---------\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>--------QUERY---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>How might the upcoming U.S. presidential election impact investment strategies?</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:green;'>--------ANSWER---------</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["Based on the document, here are the key potential investment impacts of the upcoming US presidential election:\n","\n","1. Trade Policy Implications:\n","- Both candidates are likely to pursue additional export controls, especially in advanced technology\n","- Harris is expected to maintain current tariff policies with potential targeted China tariffs\n","- Trump proposes more aggressive tariffs (60% on China, 10-20% broad tariffs)\n","- Increased protectionism under either administration could contribute to structural inflation\n","\n","2. Market Volatility:\n","- Election uncertainty could lead to increased market volatility heading into November\n","- Alpha-seeking strategies could help capitalize on stock-level dispersion during volatile periods\n","- UK equities are suggested as a potential hedge against trade rhetoric, given their low beta to shifts in global trade\n","\n","3. Fiscal Policy:\n","- Neither candidate has prioritized addressing the budget deficit\n","- Analysis suggests either candidate could pursue fiscally expansionary policies\n","- This could bring volatility to the long end of the US Treasury curve\n","- Higher fiscal deficits could lead to increased bond issuance and higher 10-year yields\n","\n","4. Portfolio Strategies:\n","- The document suggests considering:\n","  - Quality-focused investments to manage volatility\n","  - Active management approaches to navigate uncertainty\n","  - Diversification through alternative assets\n","  - Maintaining selective positioning across asset classes\n","\n","The document emphasizes that control of Congress will be key for policy implementation, and suggests investors should prepare for potential market volatility while maintaining a selective, quality-focused approach across their portfolios.\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>--------TOKENS COUNT---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>BetaUsage(cache_creation_input_tokens=0, cache_read_input_tokens=31019, input_tokens=32, output_tokens=333)</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n","--------STOP REASON---------\n","end_turn\n","\n","\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:red;'>Query n° 4</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["--------QUERY---------\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>--------QUERY---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>What are BlackRock’s views on the impact of trade policies and economic fragmentation on inflation?</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:green;'>--------ANSWER---------</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["Based on the document, BlackRock views trade policies and economic fragmentation as key structural factors that could keep inflation higher over the medium term. Specifically:\n","\n","1. Trade Policy Impact:\n","- Both US presidential candidates are likely to pursue additional export controls, especially in advanced technology\n","- Under Harris, they expect maintenance of status quo with potential for targeted China tariffs\n","- Under Trump, they note proposed 60% tariffs on China and 10-20% broad tariffs would be \"a major escalation\"\n","\n","2. Economic Fragmentation:\n","- The document states that \"Increased protectionism under either administration reinforces geopolitical and economic fragmentation\"\n","- This fragmentation is cited as \"one of the structural factors we see keeping inflation higher over the medium term\"\n","\n","3. Related Factors:\n","- They note reduced legal immigration under either administration could impact labor markets\n","- The combination of trade restrictions and economic fragmentation appears to contribute to their view of \"sticky inflation\"\n","- This factors into their broader market outlook and investment positioning\n","\n","4. Investment Implications:\n","- BlackRock suggests considering UK equities as a potential hedge against trade rhetoric, noting UK equity returns have low sensitivity to shifts in global trade\n","- They recommend maintaining selective positioning across asset classes given these structural risks\n","\n","The overall message is that BlackRock sees trade policies and economic fragmentation as persistent forces that will likely contribute to a higher inflation environment going forward, regardless of election outcomes.\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>--------TOKENS COUNT---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>BetaUsage(cache_creation_input_tokens=0, cache_read_input_tokens=31019, input_tokens=37, output_tokens=310)</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n","--------STOP REASON---------\n","end_turn\n","\n","\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:red;'>Query n° 5</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["--------QUERY---------\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>--------QUERY---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:fuchsia;'>How does BlackRock suggest positioning a portfolio to mitigate geopolitical risks?</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:green;'>--------ANSWER---------</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["Based on the document, BlackRock suggests several key approaches to mitigate geopolitical risks in portfolios:\n","\n","1. Gold Exposure:\n","- They view gold as a potential hedge against geopolitical risk\n","- Note strong demand and sentiment shift with $7.8B added to gold exposures globally since May\n","- Suggest using physical gold exposure to bolster portfolio resilience\n","\n","2. Quality Focus:\n","- Maintain focus on high-quality exposures at the core of portfolios\n","- Recommend high-conviction alpha strategies in both ETF and mutual fund formats\n","- Quality factor remains the most popular factor this year with $24.9B in flows\n","\n","3. Diversification Through Alternatives:\n","- Recommend liquid alternatives as an effective hedge against volatility spikes and economic shocks\n","- Suggest macro alternatives strategies that position long-short across countries\n","- Look to capture idiosyncratic risk through alternative investments\n","\n","4. Regional Positioning:\n","- Favor UK equities as a potential insulator against trade tensions, noting their low beta to shifts in global trade\n","- Take a selective, active approach in European markets\n","- Maintain granular approach to emerging markets given varying geopolitical environments\n","\n","5. Multi-Asset Strategies:\n","- Recommend high-conviction, unconstrained multi-asset strategies to navigate market complexities\n","- Use these to help manage through interconnected market shocks and information flows\n","- Focus on dynamic asset allocation to respond to changing conditions\n","\n","The overall emphasis appears to be on building defensive positioning through quality assets, alternative strategies, and appropriate diversification while maintaining flexibility to adjust to changing conditions.\n","\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>--------TOKENS COUNT---------</p>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<p style='color:blue;'>BetaUsage(cache_creation_input_tokens=0, cache_read_input_tokens=31019, input_tokens=35, output_tokens=354)</p>"]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["\n","--------STOP REASON---------\n","end_turn\n","\n","\n","\n"]}]},{"cell_type":"markdown","source":["## **Takeaways**"],"metadata":{"id":"Y8Hj8snQPUGt"}},{"cell_type":"markdown","source":["Here is the completion.usage of the second query in the list (which corresponds to query 1):\n","```\n","--------TOKENS COUNT---------\n","\n","BetaUsage(cache_creation_input_tokens=0, cache_read_input_tokens=31019, input_tokens=38, output_tokens=378)\n","\n","```\n","\n","\n","\n","We can see clearly that the cache tokens were used with **cache_read_input_tokens=31019**"],"metadata":{"id":"aDgrq1hGJcK-"}},{"cell_type":"markdown","source":["**Cost and Number of tokens:**\n","\n","The calls with prompt caching costs me 0.19\\$ for the 6 calls\n","Which corresponds to 0.0316$ per call (vs 0.10\\$ without prompt caching).\n","I've sent 188326 tokens for the 6 queries ==> average 31 387 tokens per call, including the pdf and the query.\n","\n","However, since the PDF tokens were cached, they weren’t processed again when resent, which helped lower the cost.\n","\n","**For my humbe use case, using prompt caching reduced my cost by almost 70%!**"],"metadata":{"id":"Mg8JMd0QK1SF"}}]}